const Controller=require("egg").Controller
const PageModel=require("../model/PageModel");
const MessageBox=require("../utils/MessageBox");

class CategoryInfoController extends Controller{
    async list(){
        let result =await this.ctx.service.categoryInfo.getListByPage(this.ctx.request.query);
        await this.ctx.render("categoryInfo/list",{
            categoryInfoList:result[0],
            pageModel:new PageModel(result[1][0].totalCount,this.ctx.query.pageIndex||1)
        });
    }
    async addCategoryInfo(){
        await this.ctx.render("categoryInfo/addCategoryInfo");
    }
    async doAddCategoryInfo(){
        let flag =await this.ctx.service.categoryInfo.addCategoryInfo(this.ctx.request.body);
        if(flag){
            this.ctx.body=MessageBox.alertAndRedirect("新增成功","/categoryInfo/list");
        }
        else{
            this.ctx.body=MessageBox.alertAndBack("新增失败");
        }
    }

    async editCategoryInfo(){
        let categoryid=this.ctx.params.categoryid;
        let result = await this.ctx.service.categoryInfo.findByCategoryId(categoryid);
        if(result){
            await this.ctx.render("categoryInfo/editCategoryInfo",{
                categoryInfo:result
            });
        }
        else{
            this.ctx.body=MessageBox.alertAndBack("查不到数据，请返回刷新重试");
        }
    }

    async doEditCategoryInfo(){
        let flag= await this.ctx.service.categoryInfo.editCategoryInfo(this.ctx.request.body);
        if(flag){
            this.ctx.body=MessageBox.alertAndRedirect("修改成功","/categoryInfo/list");
        }
        else{
            this.ctx.body=MessageBox.alertAndBack("修改失败，请返回重试");
        }
    }
    async deleteCategory(){
        //得到参数，转换成数组
        let cksValue=JSON.parse(this.ctx.request.query.cksValue);
        let flag =await this.ctx.service.categoryInfo.deleteCategoryList(cksValue);
        this.ctx.body=flag;
    }
}

module.exports=CategoryInfoController;